package cn.edu.sctu.sctu_lysd.view.act;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.List;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.adapter.AreaListAda;
import cn.edu.sctu.sctu_lysd.bean.Area;
import cn.edu.sctu.sctu_lysd.utils.DBhelper;
import cn.edu.sctu.sctu_lysd.utils.ScreenUtils;

/**
 * Created by wangfuxin on 2017-04-06.
 */

public class IndexPlacePopWin extends PopupWindow {

    private Context context;
    private View contentView;
    private ListView mListView;
    private View view;

    private List<Area> provinceList;
    private List<Area> cityList;
    private List<Area> districtList;
    private DBhelper dBhelper;

    private TextView provinceName, cityName, districtName;
    private AreaListAda mAdapter;

    private int nowPage = 1;

    private selectOver selectOver;

    public void setSelectOver(IndexPlacePopWin.selectOver selectOver) {
        this.selectOver = selectOver;
    }

    public IndexPlacePopWin(View contentView, int width, int height, boolean focusable, Context context, View view) {
        super(contentView, width, height, focusable);
        this.context = context;
        this.contentView = contentView;
        this.view = view;

        mListView = (ListView) contentView.findViewById(R.id.area_list);
        provinceName = (TextView) contentView.findViewById(R.id.provinceName);
        cityName = (TextView) contentView.findViewById(R.id.cityName);
        districtName = (TextView) contentView.findViewById(R.id.districtName);

        this.setContentView(contentView);

        this.setTouchable(true);

        this.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
        this.setWidth(ScreenUtils.getScreenWidth(context));
        this.setHeight(ScreenUtils.getScreenHeight(context));


        this.setAnimationStyle(R.style.index_pup_win);

        ColorDrawable dw = new ColorDrawable(0xff0000);
        this.setBackgroundDrawable(dw);

        areaSelect();


    }

    public void showAsDropDown() {
        this.showAsDropDown(view, 0, 0);
    }

    private void areaSelect() {

        dBhelper = new DBhelper(context);

        provinceList = dBhelper.getProvince();

        mAdapter = new AreaListAda(context, provinceList);

        mListView.setAdapter(mAdapter);


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (nowPage == 1) {

                    cityList = dBhelper.getCity(provinceList.get(position).getCode());

                    provinceName.setText(provinceList.get(position).getName());

                    mAdapter.dataChange(cityList);

                    cityName.setText("请选择");

                    initColor(2);

                }
                if (nowPage == 2) {

                    districtList = dBhelper.getDistrict(cityList.get(position).getCode());

                    cityName.setText(cityList.get(position).getName());

                    mAdapter.dataChange(districtList);

                    districtName.setText("请选择");

//                    int totalHeight = 0;
//                    for (int i = 0; i < mAdapter.getCount(); i++) {
//                        View listItem = mAdapter.getView(i, null, mListView);
//                        listItem.measure(0, 0);
//                        totalHeight += listItem.getMeasuredHeight();
//                    }
//
//                    ViewGroup.LayoutParams params = mListView.getLayoutParams();
//                    params.height = totalHeight + (mListView.getDividerHeight() * (mAdapter.getCount() - 1));
//
//                    mListView.setLayoutParams(params);

                    initColor(3);

                }

                if (nowPage == 3) {

                    districtName.setText(districtList.get(position).getName());

                    if (selectOver != null) {

                        selectOver.selectOver(provinceName.getText().toString() +
                                cityName.getText().toString() +
                                districtName.getText().toString());

                    }


                }

                if (nowPage < 3)
                    nowPage++;


            }
        });


    }

    private void initColor(int flag) {

        switch (flag) {

            case 1:
                provinceName.setTextColor(android.graphics.Color.RED);
                break;
            case 2:

                provinceName.setTextColor(Color.BLACK);
                cityName.setTextColor(Color.RED);
                provinceName.setTextColor(Color.BLACK);

                break;
            case 3:

                provinceName.setTextColor(Color.BLACK);
                cityName.setTextColor(Color.BLACK);
                districtName.setTextColor(Color.RED);

                break;
            default:
                break;

        }


    }

    public interface selectOver {

        void selectOver(String place);

    }


}
